package exceptionHandleTest;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.bcel.classfile.ClassParser;
import org.apache.bcel.classfile.JavaClass;
import org.apache.bcel.classfile.Method;
import org.apache.bcel.generic.ClassGen;


public class BcelTestEH {
	
	private String ModifyClassName;
	private String methodName;
	
	 private static void addWrapper(ClassGen cgen, Method method) {
		 
		 System.out.println("addWrapper");
	 }
	 
	 public static void main(String[] argv) {
		 String ModifyClassName = "bin\\TestEH.class";
		 String methodName="main";
		// System.out.println(ModifyClassName);
		 if (ModifyClassName.endsWith(".class")) {
	          try {
	        	  JavaClass jclas = new ClassParser(ModifyClassName).parse();
	              ClassGen cgen = new ClassGen(jclas);
	              Method[] methods = jclas.getMethods();
	              int index;
	              for (index = 0; index < methods.length; index++) {
	                  if (methods[index].getName().equals(methodName)) {
	                      break;
	                  }
	              }
	              if (index < methods.length) {
	                  addWrapper(cgen, methods[index]);
	                  FileOutputStream fos =
	                        new FileOutputStream(ModifyClassName);
	                  cgen.getJavaClass().dump(fos);
	                  fos.close();
	              } else {
	                  System.err.println("Method " + methodName + 
	                      " not found in " + ModifyClassName);
	              }
	          } catch (IOException ex) {
	                ex.printStackTrace(System.err);
	          }
	            
	      } else {
	          System.out.println("Usage: BCELTiming class-file method-name");
	      }
	 }
}
